package com.example.yangxu0703.thundermapclient.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.example.yangxu0703.thundermapclient.iterface.DecodeCallback;

/**
 * Created by yangxu0703 on 18-3-18.
 */

public class ConsumerThread extends Thread {

    public DecodeCallback decodeCallback;

    public void setDecodeCallback(DecodeCallback decodeCallback) {
        this.decodeCallback = decodeCallback;
    }

    @Override
    public void run() {
        super.run();
        while (true) {
            if (!ProduceThread.startdecode) {
                Log.i("Consumer","Thread start decode image!");
                try {
                    sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long tem_start = System.currentTimeMillis();
                Log.i("Consumer", "" + tem_start);
                Bitmap bitmap = BitmapFactory.decodeByteArray(ProduceThread.SendBuff, 0, ProduceThread.SendBuff.length);
                long diff_time = System.currentTimeMillis() - tem_start;
                Log.i("Consumer", "" + diff_time);
                decodeCallback.decodefinish(bitmap);
                ProduceThread.startdecode = true;
                synchronized (ProduceThread.class){
                    ProduceThread.class.notify();
                }
            } else {
                Log.i("Consumer","Thread wait!");
                synchronized (ConsumerThread.class){
                    try {
                        ConsumerThread.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

        }
    }

}
